<template>
	<view class="">
		<!-- v-model="dataInfo.list" @query="queryList" -->
		<z-paging ref="refPaging">
			<template #top>
				<!-- bgColor="transparent" -->
				<uv-navbar :fixed="false" placeholder safeAreaInsetTop bgColor="#fff">
					<template v-slot:left>
						<view class="" @click.stop=" addOpen('/pages/mine/set')">
							<uv-image src="/static/images/shezhi.png" width="19px" height="19px"
								mode="scaleToFill"></uv-image>
						</view>
					</template>
					<template v-slot:center>
						<view class="Flex bold" style="font-size: 16px">我的</view>
					</template>
				</uv-navbar>
			</template>
			<view class="bgMinor2 pt-24 plr-24 ">
				<view class="">


					<!--   getUserFun(1,2,1)-->
					<view class="center relative" @click="submitData()">
						<view v-if="dataInfo && dataInfo.userInfoObj && dataInfo.userInfoObj.avatar"
							class="width-240 height-240" style="overflow: hidden;border-radius: 50%;">
							<image :src="dataInfo.userInfoObj.avatar" class="width-240 height-240"></image>
							<!-- <view class="width-240 height-240 bgWhite radius-circle" style="overflow: hidden;display: flex;align-items: center;">
							
							</view>
							 -->
						</view>
						<view v-else>
							<view class="width-240 height-240 bgWhite radius-circle"
								style="display: flex;justify-content: center;align-items: center;">

								<view class=" color-black2">点击完善资料</view>
							</view>

						</view>
					</view>
					<view class="center mt-24" style="position: relative;left: 30rpx;">
						<view class="bold mr-24 size-36" style="margin-right: 0px;">
							{{dataInfo.userInfoObj ? dataInfo.userInfoObj.username:''}}
							<text
								v-show="dataInfo.userInfoObj && dataInfo.userInfoObj.usernames && dataInfo.userInfoObj.usernames.length > 5">
								...</text>
						</view>
						<view style="margin-left: 1vh;">
							<uv-image v-if="dataInfo.isMember == 1" src="/static/images/vip2.png" width="44rpx" height="44rpx" mode="scaleToFill"></uv-image>
							<uv-image v-else style='opacity: 0 !important;' src="/static/images/san.png" width="44rpx" height="44rpx"
								mode="scaleToFill"></uv-image>
						</view>
					</view>
					<!-- @click.stop="addOpen(`/pages/mine/openVip`)" -->
					<view class="mt-16 tc color color-black5" v-show="dataInfo.isMember != 1"  @click="openMembership()" >开会员 享权益
					</view>
				</view>
			</view>

			<view class="plr-24 pt-40 pb-24 bgMinor2">
				<view class="bgWhite pt-20 pb-40 radius-16">
					<view class="">
						<view class="flex color-black5 size-38">
							<view class="flex-1 tc tabItem height-76 border-b-1" style="z-index: 111;" @click.stop="handleTabs(0)">
								<view class="Flex">
									<view class="tc line-hiding-1 mr-16"
										:class="{ 'color-black': dataInfo.current == 0, 'weight-6': dataInfo.current === 0 }">
										我</view>
								</view>
								<view v-show="dataInfo.current === 0" class="height-6 width100"
									style="background-color: #007df5; margin: 0 auto; margin-top: 20rpx"></view>
							</view>
							<view class="flex-1 tabItem height-76 border-b-1" @click.stop="handleTabs(1)">
								<view class="Flex">
									<view class="tc line-hiding-1 mr-16"
										:class="{ 'color-black': dataInfo.current == 1, 'weight-6': dataInfo.current === 1 }">
										收藏</view>
								</view>
								<view v-show="dataInfo.current == 1" class="height-6 width100"
									style="background-color: #007df5; margin: 0 auto; margin-top: 20rpx"></view>
							</view>
						</view>
					</view>

					<view v-if="dataInfo.current == 0" class="">
						<scroll-view scroll-y style="height: 55vh;" @scrolltolower="onScrollToLower">
							<!-- 嵌套子组件 -->
							<mine-me ref="childComp" />
							<!-- 模拟一些内容 -->
							<!-- <view v-for="i in 1" :key="i" style="visibility: hidden">{{ i }}</view> -->
						</scroll-view>
					</view>
					<view v-if="dataInfo.current == 1" class="">
						<scroll-view scroll-y style="height: 55vh;" @scrolltolower="onScrollToLowers">
							<!-- 嵌套子组件 -->
							<mine-collect ref="childCompaa" />
							<!-- 模拟一些内容 -->
							<view v-for="i in 1" :key="i" style="visibility: hidden">{{ i }}</view>
						</scroll-view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script setup lang="ts">
	import { reactive, onMounted, ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { getWindowHeight } from '@/pages/util/getHeight';
	import { addOpen, loggedIn, addOpenTab } from '@/pages/util/hooks';
	import mineMe from "./component/mine-me"
	import mineCollect from "./component/mine-collect"
	import useLoginStore from '@/pinia-store/userInfoStore';
	import { getExampleData, getUser, weCatlogins } from "@/utils/api.js";
	import axios from 'axios';
	import store from '@/store';
	import { get, post } from '@/utils/request';
	import { onShow } from '@dcloudio/uni-app';
	const useLogin = useLoginStore();
	// console.log('useLoginuseLogin',useLogin);
	/** @name 数据 */
	const dataInfo = reactive({
		current: 0,
		userInfoObj: {
			avatar: '',
			username: '',
		}
	});
	function onScrollToLowers(){
		childCompaa.value.loadMoreData();
	}
	function onScrollToLower() {
		childComp.value.loadMoreData();
	}
	function handleTabs(index : number) {
		dataInfo.current = index;
		if (dataInfo.current == 0) {
			// childComp.value?.listFun();

		} else {
			// childCompaa.value?.fetchData();
		}
	}
	// 掉微信
	const submitData = async () => {
		try {
			// 获取 code
			const loginRes = await uni.login({
				provider: 'weixin',
			});

			// 获取用户信息
			const userInfoRes = await uni.getUserInfo({
				provider: 'weixin',
				lang: 'zh_CN',
			});
			getUserFun(loginRes.code, userInfoRes.userInfo.avatarUrl, userInfoRes.userInfo.nickName)
			// userInfo.value = userInfoRes.userInfo;
			console.log('获取到的用户信息:', userInfoRes.userInfo);

		} catch (error) {
			console.error('获取 code 或用户信息时出错:', error);
		}
	};
	// 直接请求登录
	const getUserFun = async (code : any, avatarUrl : any, realname : any) => {
		try {

			const data = {
				avatarUrl: avatarUrl,
				signature: code,
				inviteCode: "1"
			}
			// uni.request({
			// 	data: data,
			// 	url: 'http://8.138.81.159:5301/ai/sys/weCatlogins', // 替换为实际的 API 地址
			// 	method: 'POST',
			// 	success: (res) => {
			// 		const responses = res.data.result;
			// 		console.log('提交结果:', responses);
			// 		uni.setStorageSync('Token', res.data.result.token);
			// 		// sessionStorage.setItem('Token', res.data.result.token);
			// 		dataInfo.userInfoObj = responses.userInfo


			// 		dataInfo.userInfoObj.usernames = responses.userInfo.username
			// 		dataInfo.userInfoObj.username = responses.userInfo.username.substring(0, 5)
			// 		console.log('responses.userInfo')
			// 		console.log(dataInfo.userInfoObj)
			// 		store.commit('setIsMember', responses.userInfo.isMember)
			// 		// "isMember":"是否会员  0：不是 1：是",
			// 		store.commit('setisMerchant', responses.userInfo.isMerchant)
			// 		// "isMerchant":"是否商家 0：不是 1：是",
			// 		uni.setStorageSync('userData', JSON.stringify(responses.userInfo));
			// 		// sessionStorage.setItem('userData', JSON.stringify(responses.userInfo))
			// 	},
			// 	fail: (err) => {
			// 		console.error('请求失败:', err);
			// 	}
			// });
			// // console.log(data)



		} catch (error) {
			// 处理错误
		}
	}
	const fetchData = async () => {
		try {
			const response = await get<{
				proportion : Number;
			}>('maixing/getList', { type: 1 });
			if (response) {

			}

		} catch (error) {
			console.error(error);
		}
	};

	const onChooseavatar = (e) => {
		if (uni.getUserProfile) {
			uni.getUserProfile({
				desc: "获取用户信息仅用于展示",
				success(res) {
					console.log(res);
					console.log("用户点击允许");
					console.log("用户信息", that.userInfo);
					const userInfo = res.userInfo;
					//性别 0：未知、1：男、2：女

				},
				fail(error) {
					console.log("用户未授权", error);
					// uni.showToast({
					//   title: "登录失败",
					//   duration: 3000,
					//   icon: "none",
					// });
				},
			});
		};
	}
	const childComp = ref(null)
	const childCompaa = ref(null)

	const getUser = async () => {
		try {
			const response = await get<{
				result : any;
			}>('sys/user/getUser');
			if (response) {
				dataInfo.userInfoObj.avatar = response.result.avatar
				dataInfo.userInfoObj.username = response.result.realname
				dataInfo.isMember =  response.result.isMember
				
				console.log(response)
			}

		} catch (error) {
			console.error(error);
		}
	};


	onMounted(() => {
		getUser()
		// dataInfo.userInfoObj.avatar = uni.getStorageSync('avatarUrl');
		// dataInfo.userInfoObj.username = uni.getStorageSync('username');

		if (dataInfo.current == 0) {
			childComp.value?.listFun();
		} else {
			childCompaa.value?.fetchData();
		}

	})
	const openMembership = async () => {
		try {
			uni.navigateTo({
			               url: `/pages/subscribeMember/index`, // 目标页面路径（需在pages.json注册）
			               success: () => console.log('跳转成功'),
			               fail: (err) => console.error('跳转失败', err)
			           })

		} catch (error) {
			console.error(error);
		}
	};
	// onShow(() => {
	// 	fetchData()
	// });
	// onLoad((options : any) => {
	// 	fetchData()
	// })
</script>

<style scoped lang="scss">
	onShow // :deep(.checklist-box) {
	// 	//  模版
	// }

	.ziliao {
		position: absolute;
	}
</style>